package com.zjt.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjt.bean.Admin;
import com.zjt.bean.AdminExample;
import com.zjt.bean.AdminReq;
import com.zjt.bean.BaseReq;
import com.zjt.bean.data.BaseData;
import com.zjt.mapper.AdminMapper;
import com.zjt.util.CskaoyanPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    AdminMapper adminMapper;
    @Override
    public BaseData<Admin> query(BaseReq baseReq) {//*
        //问题：我是要在service这里写业务，还是去mybatis的映射文件里写sql语句
        CskaoyanPageHelper.startPage(baseReq);//*

        AdminReq adminReq = (AdminReq) baseReq;
        String username = adminReq.getUsername();
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }

        List<Admin> admins = adminMapper.selectByExample(adminExample);

        PageInfo<Admin> adminPageInfo = new PageInfo<>(admins);
        long total = adminPageInfo.getTotal();

        return BaseData.ok(admins,total);
    }
}
